package com.wnx.job.modules.pms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnx.job.modules.jms.model.JmsCompany;
import com.wnx.job.modules.jms.service.JmsCompanyService;
import com.wnx.job.modules.pms.mapper.PmsAdvantageMapper;
import com.wnx.job.modules.pms.mapper.PmsCompanyAdvantageRelationMapper;
import com.wnx.job.modules.pms.model.PmsAdvantage;
import com.wnx.job.modules.pms.model.PmsCompanyAdvantageRelation;
import com.wnx.job.modules.pms.service.PmsAdvantageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class PmsAdvantageServiceImpl extends ServiceImpl<PmsAdvantageMapper, PmsAdvantage> implements PmsAdvantageService {
    @Resource
    private JmsCompanyService companyService;
    @Resource
    private PmsAdvantageMapper advantageMapper;
    @Resource
    private PmsCompanyAdvantageRelationMapper advantageRelationMapper;


    /**
     * 添加公司优势
     * @param advantageIds
     * @param username
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAdvantage(long[] advantageIds, String username) {
        List<PmsCompanyAdvantageRelation> container = null;

        //先删除旧的公司优势关联
        JmsCompany companyByDb = companyService.findByUsername(username);
        long count = this.findCountByCompanyId(companyByDb.getId());
        if (count > 0){this.deleteByCompanyId(companyByDb.getId());}


        //构造公司优势集合
        if (advantageIds!=null && advantageIds.length > 0){
            container = new ArrayList<>();
            for (long advantageId : advantageIds) {
                PmsCompanyAdvantageRelation x = new PmsCompanyAdvantageRelation();
                x.setCompanyId(companyByDb.getId());
                x.setAdvantageId(advantageId);
                container.add(x);
            }
        }


        //遍历依次添加
        for (PmsCompanyAdvantageRelation x : container) {
            advantageRelationMapper.insert(x);
        }
    }

    /**
     * 根据公司ID查公司优势集合
     * @param companyId
     * @return
     */
    @Override
    public List<PmsCompanyAdvantageRelation> findCompanyAdvantageRelationsByCompanyId(Long companyId) {
        return advantageRelationMapper.selectList(new QueryWrapper<PmsCompanyAdvantageRelation>().eq("company_id",companyId));
    }

    /**
     * 根据公司ID查询公司优势
     * @param companyId
     * @return
     */
    @Override
    public List<PmsAdvantage> findAdvantageListByCompanyId(Long companyId) {
        return advantageMapper.findAdvantageListByCompanyId(companyId);
    }

    /**
     * 根据公司ID查询优势数量
     * @param companyId
     * @return
     */
    private long  findCountByCompanyId(long companyId){
        return advantageRelationMapper.selectCount(new QueryWrapper<PmsCompanyAdvantageRelation>().eq("company_id",companyId));
    }

    /**
     * 根据公司Id删除优势
     * @param companyId
     */
    private void deleteByCompanyId(long companyId){
        advantageRelationMapper.delete(new QueryWrapper<PmsCompanyAdvantageRelation>().eq("company_id",companyId));
    }
}
